<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的-社会化登录功能" tabindex="-1"><a class="header-anchor" href="#laravel-的-社会化登录功能"><span>Laravel 的 社会化登录功能</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#license">授权</a></li>
<li><a href="#official-documentation">官方文档</a></li>
<li><a href="#configuration">配置</a></li>
<li><a href="#basic-usage">基本用法</a>
<ul>
<li><a href="#stateless-authentication">无状态身份验证</a></li>
<li><a href="#retrieving-user-details">检索用户详细信息</a></li>
<li><a href="#retrieving-user-details-from-token">从令牌检索用户详细信息</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 社会化登录通过 Facebook ， Twitter ，Google ，LinkedIn ，GitHub 和 Bitbucket 提供了一个富有表现力的，流畅的 OAuth 身份验证界面。它几乎能处理所有你害怕处理的各种样板社会认证代码。</p>
<blockquote>
<p>翻译自 Readme： <a href="https://github.com/laravel/socialite" target="_blank" rel="noopener noreferrer">github.com/laravel/socialite</a></p>
</blockquote>
<p><strong>我们不接受新的适配器。</strong></p>
<p><strong>如果你使用 Laravel 5.3 或更低版本，请使用 <a href="https://github.com/laravel/socialite/tree/2.0" target="_blank" rel="noopener noreferrer">Socialite 2.0</a>。</strong></p>
<p>社区驱动的社会化登录提供商网站上可以找到为其他平台提供的适配器列表。</p>
<h2 id="授权" tabindex="-1"><a class="header-anchor" href="#授权"><span>授权</span></a></h2>
<p>Laravel 社会化登录是根据 <a href="http://opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">MIT 授权</a> 许可的开源软件</p>
<h2 id="官方文档" tabindex="-1"><a class="header-anchor" href="#官方文档"><span>官方文档</span></a></h2>
<p>除了常规的基于表单的身份验证之外， Laravel 也提供了一种简单，方便的办法来使用 <a href="https://github.com/laravel/socialite" target="_blank" rel="noopener noreferrer">Laravel 社会化登录</a> 向 OAuth 提供程序进行身份验证。社公化登录目前支持 <code v-pre>Facebook</code> ， <code v-pre>Twitter</code> ， <code v-pre>LinkedIn</code> ，<code v-pre>Google</code> ，<code v-pre>GitHub</code> 和 <code v-pre>Bitbucket</code> 的身份验证。</p>
<p>要开始社会化登录，使用 <code v-pre>composer</code> 将相应包加入到你项目的依赖项中。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>socialite</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>安装完社会化登录库之后，在你的 <code v-pre>config/app.php</code> 文件中注册 <code v-pre>Laravel\Socialite\SocialiteServiceProvider</code> 。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'providers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// Other service providers...</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name class-name-fully-qualified static-context">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>SocialiteServiceProvider</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同时，在你的 <code v-pre>app</code> 配置文件中，把 <code v-pre>Socialite</code> facade 加入到 <code v-pre>aliases</code> 数组中。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'Socialite'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还需要为你应用使用的 OAuth 服务加入凭据。这些凭据应该放在你的 <code v-pre>config/services.php</code> 文件中，并且使用 <code v-pre>facebook</code> ， <code v-pre>twitter</code> ， <code v-pre>linkedin</code> ， <code v-pre>google</code> ， <code v-pre>github</code> 或 <code v-pre>bitbucket</code> 作为键名，具体取决于在你的应用中由哪个程序来提供验证服务，比如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'github'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-github-app-id'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-github-app-secret'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'redirect'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://your-callback-url'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h3>
<p>接下来，你已经准备好了验证用户了！你需要两个路由：一个重定向用户到 OAuth 提供商，另一个在提供商验证之后接收回调。我们用 <code v-pre>Socialite</code> facade 来访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">LoginController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Redirect the user to the GitHub authentication page.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">redirectToProvider</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Obtain the user information from GitHub.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handleProviderCallback</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// $user->token;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>redirect</code> 方法负责发送用户到 OAuth 提供商，而 <code v-pre>user</code> 方法将读取传入的请求并从提供商处检索用户信息。在重定向用户之前，你还可以加入附加的 <code v-pre>scope</code> 方法来设置请求的「scope」。这个方法会覆盖所有现有的范围。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">scopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'scope1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'scope2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>setScopes</code> 方法覆盖所有已经存在的 scopes：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">setScopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'scope1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'scope2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你需要定义通往你的控制器方法的路由。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login/github'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Auth\LoginController@redirectToProvider'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login/github/callback'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Auth\LoginController@handleProviderCallback'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>一部分 OAuth 提供商在重定向请求中支持携带可选参数。要在请求中包含任何可选参数，调用 <code v-pre>with</code> 方法时传入可选的数组即可。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'hd'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>当使用 <code v-pre>with</code> 方法时，注意不要传递保留关键字，比如 <code v-pre>state</code> 或 <code v-pre>response_type</code> 。</p>
<h4 id="无状态身份验证" tabindex="-1"><a class="header-anchor" href="#无状态身份验证"><span>无状态身份验证</span></a></h4>
<p><code v-pre>stateless</code> 方法可以用于禁用 session 状态的验证，这个方法在向 API 添加社会化身份验证时非常有用。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">stateless</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="检索用户详细信息" tabindex="-1"><a class="header-anchor" href="#检索用户详细信息"><span>检索用户详细信息</span></a></h4>
<p>一旦你有了一个用户实例，你可以获取这个用户的更多详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// OAuth Two Providers</span></span>
<span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$refreshToken</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">refreshToken</span><span class="token punctuation">;</span> <span class="token comment">// not always provided</span></span>
<span class="line"><span class="token variable">$expiresIn</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">expiresIn</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// OAuth One Providers</span></span>
<span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$tokenSecret</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">tokenSecret</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// All Providers</span></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getNickname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getEmail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getAvatar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从令牌检索用户详细信息" tabindex="-1"><a class="header-anchor" href="#从令牌检索用户详细信息"><span>从令牌检索用户详细信息</span></a></h4>
<p>如果你已经有了一个用户的有效访问令牌，你可以使用 <code v-pre>userFromToken</code> 方法检索用户的详细信息。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">userFromToken</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/qufo" target="_blank" rel="noopener noreferrer">@qufo</a></td>
<td><img src="https://avatars1.githubusercontent.com/u/2526883?v=3&amp;s=460?imageView2/1/w/100/h/100" alt="100"></td>
<td>翻译</td>
<td>欢迎共同探讨。<a href="https://github.com/qufo" target="_blank" rel="noopener noreferrer">@Qufo</a></td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


